/* Title:		
 *
 * Project:     MAS
 *
 * @ Link: 		http://...
 *
 * @ Email:		ivan_ling@hotmail.com
 *
 * @ Copyright: Copyright (c) 2008 mezimedia
 *
 * @ Author 	Ivan.ling
 *
 * @ Version 	1.0
 
 * @ last change time 2008-01-20 	
 */
package com.pub.util;

import com.sun.management.OperatingSystemMXBean;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.Enumeration;

public class SystemUtils
{
	public static enum EnumUnit{GB, MB, KB, B};
	
	private static OperatingSystemMXBean osmbSystemBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
	
	public static void main(String args[])
	{
		try {
			//System.out.println("local IP : " + SystemUtils.strGetLocalIP());//
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/* get current method
	 * 
	 */
	public static String getMethod()
	{
		StackTraceElement[] st = Thread.currentThread().getStackTrace();
		
		if(st.length > 2)
			return st[2].toString();
		else
			return null;
	}
	
	/* get current method name
	 * 
	 */
	public static String getMethodName()
	{
		StackTraceElement[] st = Thread.currentThread().getStackTrace();
		
		if(st.length > 2)
			return st[2].getMethodName();
		else
			return null;
	}
	
	/* get invoke method name
	 * 
	 */
	public static String getInvokeMethod()
	{
		StackTraceElement[] st = Thread.currentThread().getStackTrace();
		
		if(st.length > 3)
			return st[3].toString();
		else
			return null;
	}
	
	/* get invoke method name
	 * 
	 */
	public static String getInvokeMethodName()
	{
		StackTraceElement[] st = Thread.currentThread().getStackTrace();
		
		if(st.length > 3)
			return st[3].getMethodName();
		else
			return null;
	}
	
	public static String strGetLocalIP()
	{
	    InetAddress ip = null;

		Enumeration<NetworkInterface> netInterfaces;
		try {
			netInterfaces = NetworkInterface.getNetworkInterfaces();
			
			while(netInterfaces.hasMoreElements()) {
		    	NetworkInterface ni = netInterfaces.nextElement();
		    	
		    	ip = (InetAddress)ni.getInetAddresses().nextElement();
		    	
		    	if(!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1){
		    		break;
		    	} else {
		    		ip = null;
		       }
		    }
		} catch (SocketException e) {
			e.printStackTrace();
		}
	    
	    if(ip == null)
	    	return null;
	    else
	    	return ip.getHostAddress();
	}
	
	public static String strGetNetIP()
	{
	    InetAddress ip = null;

		Enumeration<NetworkInterface> netInterfaces;
		try {
			netInterfaces = NetworkInterface.getNetworkInterfaces();
			
			while(netInterfaces.hasMoreElements()) {
		    	NetworkInterface ni = netInterfaces.nextElement();
		    	ip = (InetAddress)ni.getInetAddresses().nextElement();
		    	
		    	if( !ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1){
		    		break;
		    	} else {
		    		ip = null;
		       }
		    }
		} catch (SocketException e) {
			e.printStackTrace();
		}
	    
	    if(ip == null)
	    	return null;
	    else
	    	return ip.getHostAddress();
	}
	
	public static File[] flDiskFree()
	{
		return File.listRoots();
	}
	
	public static void vdPrintDiskFree()
	{
		File[] roots = File.listRoots();//fetch disk partition.
        for (File file : roots) {
            System.out.println(file.getPath()+" detail :");
            System.out.println("free  = " + file.getFreeSpace()/1024/1024/1024 + "G");// free space
            System.out.println("used  = " + file.getUsableSpace()/1024/1024/1024 + "G");// use space
            System.out.println("total = " + file.getTotalSpace()/1024/1024/1024 + "G");//total space
            System.out.println();
        }
	}
	
	public static long getFreePhysicalMemorySize(EnumUnit eu)
	{
		switch(eu){
			case GB : 	return SystemUtils.osmbSystemBean.getFreePhysicalMemorySize()/1024/1024/1024;
			case MB : 	return SystemUtils.osmbSystemBean.getFreePhysicalMemorySize()/1024/1024;
			case KB : 	return SystemUtils.osmbSystemBean.getFreePhysicalMemorySize()/1024;
			
			default: return SystemUtils.osmbSystemBean.getFreePhysicalMemorySize();
		}
	}
	
	public static long getTotalPhysicalMemorySize(EnumUnit eu)
	{
		switch(eu){
			case GB : 	return SystemUtils.osmbSystemBean.getTotalPhysicalMemorySize()/1024/1024/1024;
			case MB : 	return SystemUtils.osmbSystemBean.getTotalPhysicalMemorySize()/1024/1024;
			case KB : 	return SystemUtils.osmbSystemBean.getTotalPhysicalMemorySize()/1024;
			
			default: return SystemUtils.osmbSystemBean.getTotalPhysicalMemorySize();
		}
	}
	
	public static double getSystemLoadAverage()
	{
		return SystemUtils.osmbSystemBean.getSystemLoadAverage();
	}
	
	public static String getStackTrace(Exception e)
	{
		OutputStream baOut = new ByteArrayOutputStream();
		PrintStream ps = new PrintStream(baOut);
		e.printStackTrace(ps);
		
		ps.close();
		
		return baOut.toString();
	}
}